/** @file    DialogLoadBundle.cpp
 *  @author  Alessandro Polo
 *  @version $Id: DialogLoadBundle.cpp 2488 2010-05-21 01:48:27Z alex $
 *  @brief
 * File containing methods for the wosh::gui::DialogLoadBundle class.
 * The header for this class can be found in DialogLoadBundle.h, check that file
 * for class description.
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/
 
 #include "DialogLoadBundle.h"
 #include "ui_DialogLoadBundle.h"

 #include <core/Request.h>
 #include <core/Response.h>
 #include <core/List.h>
 #include <core/WoshKernel.h>
 #include <core/MethodsCommon.h>

 #include <QtGui>


using namespace std;
namespace wosh {
 namespace gui {

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

DialogLoadBundle::DialogLoadBundle(QWidget *parent) : QDialog(parent), IWidget()
 {
	this->m_ui = new Ui::DialogLoadBundle();
	this->m_ui->setupUi(this);

	this->setWindowTitle("Load Bundle..");
	this->getWidgetURI().setName("DialogLoadBundle");
	this->getTargetURI().setAny();

	connect(this->m_ui->buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
	connect(this->m_ui->buttonBox, SIGNAL(rejected()), this, SLOT(reject()));

	connect(this->m_ui->listWidget, SIGNAL(itemClicked(QListWidgetItem*)), this, SLOT(itemClicked(QListWidgetItem*)));

	refreshAll();
 }

DialogLoadBundle::~DialogLoadBundle()
 {
	delete this->m_ui;
 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

void DialogLoadBundle::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        m_ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

void DialogLoadBundle::refreshAll() {
	 this->m_ui->listWidget->clear();

	List<BundleAllocator*>& bundleAllocators = WoshKernel::getInstance()->bundles().getBundleAllocator();

	bundleAllocators.mutex().lockForRead();
	if ( bundleAllocators.size_() > 0 ) {
		std::vector<BundleAllocator*>::const_iterator it;
		std::vector<BundleAllocator*>::const_iterator it_end = bundleAllocators.stdEnd();
		for ( it=bundleAllocators.stdBegin(); it!=it_end; it++ ) {
			if ( *it == NULL ) continue;
			std::vector<std::string> obj_list;
			(*it)->getSupportedObjects(obj_list);

			std::vector<string>::const_iterator its;
			for ( its=obj_list.begin(); its!=obj_list.end(); its++ ) {
				QListWidgetItem* item = new QListWidgetItem(this->m_ui->listWidget);
				item->setText( (*its).c_str() );
				item->setData(Qt::ToolTipRole, (*it)->getName().c_str() );
				this->m_ui->listWidget->addItem( item );
			 }
		 }
	 }
	bundleAllocators.mutex().unLock();
 }

void DialogLoadBundle::itemClicked( QListWidgetItem* item )
 {
	if ( item == NULL ) return;

	this->itemName = string(item->text().toLatin1().data());
	this->itemAllocator = string(item->data(Qt::ToolTipRole).toString().toLatin1().data());

 }

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////

void DialogLoadBundle::busMessage( const Message& message, const Bus* source )
 {
	(void)source;
	if ( message.isEmpty() ) return;


 }


//////////////////////////////////////////////////////////////////////////////////////////////////////////////

 }; // namespace gui
}; // namespace wosh
